package csc.fresher.view;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.dao.impl.CustomerDaoImpl;
import csc.fresher.dao.impl.SavingAccountDaoImpl;
import csc.fresher.entity.Currency;
import csc.fresher.entity.Customer;
import csc.fresher.entity.CustomerAccount;
import csc.fresher.entity.InterestRate;
import csc.fresher.entity.PeriodType;
import csc.fresher.entity.SavingTransactionType;
import csc.fresher.service.CurrencyService;
import csc.fresher.service.InterestRateService;
import csc.fresher.service.PeriodTypeService;
import csc.fresher.service.SavingAccountService;
import csc.fresher.service.SavingTransactionService;
import csc.fresher.service.impl.SavingInterestRateServiceImpl;

@Component
@Scope("session")
public class SavingCreateTransManagedBean {

	@Autowired
	PeriodTypeService periodTypeService;
	@Autowired
	CurrencyService currencyService;
	@Autowired
	SavingAccManagedBean accManagedBean;
	@Autowired
	SavingTransactionService savingTransactionServices;
	@Autowired
	SavingInterestRateServiceImpl savingInterestRateServiceImpl;
	@Autowired
	SavingAccountDaoImpl savingAccountDaoImpl;
	@Autowired
	CustomerDaoImpl customerDaoImpl;
	@Autowired
	SavingAccountService savingAccountService;
	@Autowired
	InterestRateService interestRateService;
	private String message = "";
	private CustomerAccount customerAccount;
	private Customer customer;
	private double funds;
	private SavingTransactionType savingTransactionType;
	private double currentBalance;
	private double afterBalance;
	private double newInterestRate;
	private int currencyID;
	private int periodTypeID;
	private Timestamp expiredDate;
	private List<InterestRate> savingInterestRates;
	public List<InterestRate> getSavingInterestRates() {
		return savingInterestRates;
	}

	public void setSavingInterestRates(List<InterestRate> savingInterestRates) {
		this.savingInterestRates = savingInterestRates;
	}

	@PostConstruct
	public void init() {
		savingTransactionType = SavingTransactionType.SAVING;
		currencyID = 1;
		periodTypeID = 1;
		funds = 0;
		if (accManagedBean != null) {
			customerAccount = accManagedBean.getSelectedAccount();
			setCustomer(accManagedBean.getCustomer());
			if(customerAccount != null){
				System.out.println("not null");
			} else {
				customerAccount = savingAccountDaoImpl.getCustomerAccountByID(323456789030L);
				setCustomer(customerDaoImpl.getCustomerByID(customerAccount.getCustomerID()));
				System.out.println("null");
			}
		}
	}

	public void getCurrency() {
		if (interestRateService != null&& currencyID==1) {
			savingInterestRates = interestRateService.getAllInterestRateByCurrencyID(1);
			if (savingInterestRates != null) {
				for (InterestRate i : savingInterestRates) {
					if (currencyService != null) {
						i.setCurrency(currencyService.getCurrencyByID(i.getCurrencyID()));
						System.out.println(i.getCurrencyID());
					} else {
						System.err.println("currency service is null");
					}
					if (periodTypeService != null) {
						i.setPeriodType(periodTypeService.getPeriodTypeByID(i
								.getPeriodTypeID()));
					} else {
						System.err.println("period service is null");
					}
				}
			}
		}
		
		if (interestRateService != null && currencyID==2 ) {
			savingInterestRates = interestRateService.getAllInterestRateByCurrencyID(2);
			if (savingInterestRates != null) {
				for (InterestRate i : savingInterestRates) {
					if (currencyService != null) {
						i.setCurrency(currencyService.getCurrencyByID(i.getCurrencyID()));
						System.out.println(i.getCurrencyID());
					} else {
						System.err.println("currency service is null");
					}
					if (periodTypeService != null) {
						i.setPeriodType(periodTypeService.getPeriodTypeByID(i
								.getPeriodTypeID()));
					} else {
						System.err.println("period service is null");
					}
				}
			}
		}
		else {
			System.err.println("interest rate service is null");
		}
	}
	
	
	public double getFunds() {
		return funds;
	}

	public void setFunds(double funds) {
		this.funds = funds;
	}
	
	
	public CustomerAccount getCustomerAccount() {
		return customerAccount;
	}

	public void setCustomerAccount(CustomerAccount customerAccount) {
		this.customerAccount = customerAccount;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	public List<Currency> getCurrencies() {
		return savingInterestRateServiceImpl.getAllCurrency();
	}
	
	public List<PeriodType> getPeriodTypes(){
		return savingInterestRateServiceImpl.getAllPeriodTypes();
	}
	
	public SavingTransactionType[] getTransactionTypes() {
	   return SavingTransactionType.values();
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public double getAfterBalance() {
		this.afterBalance = savingInterestRateServiceImpl.calculateAfterBalance(currentBalance, funds, newInterestRate, periodTypeID, savingTransactionType);
		return this.afterBalance;
	}

	public void setAfterBalance(double afterBalance) {
		this.afterBalance = afterBalance;
	}

	public double getCurrentBalance() {
		this.currentBalance = savingAccountService.getCurrentAccountBalance(customerAccount);
		return currentBalance;
	}

	public void setCurrentBalance(double currentBalance) {
		this.currentBalance = currentBalance;
	}

	public SavingTransactionType getSavingTransactionType() {
		return savingTransactionType;
	}

	public void setSavingTransactionType(SavingTransactionType savingTransactionType) {
		this.savingTransactionType = savingTransactionType;
	}
	
	public boolean getAcceptNewRate(){
		if (savingTransactionType == SavingTransactionType.SAVING) {
			return true;
		}
		return false;
	}

	public double getNewInterestRate() {
		newInterestRate = savingInterestRateServiceImpl.getInterestRate(currencyID, periodTypeID);
		return newInterestRate;
	}
	public void setNewInterestRate(double newInterestRate) {
		this.newInterestRate = newInterestRate;
	}
	public int getCurrencyID() {
		return currencyID;
	}
	public void setCurrencyID(int currencyID) {
		this.currencyID = currencyID;
	}
	public int getPeriodTypeID() {
		return periodTypeID;
	}
	public void setPeriodTypeID(int periodTypeID) {
		this.periodTypeID = periodTypeID;
	}
	
	


	public void createTransaction() {
		long id = savingTransactionServices.createTransaction(funds, customerAccount.getAccountNumber(), currentBalance, 1, 1, savingTransactionType, currencyID, new Timestamp(System.currentTimeMillis()), expiredDate);
		//long id = saving
		System.out.println(id);
		if(id > 0){
			System.err.println("Insert successfully!");
		} 
	}



	public Timestamp getExpiredDate() {
		expiredDate = savingInterestRateServiceImpl.getExpiryDate(periodTypeID);
		return expiredDate;
	}

	public void setExpiredDate(Timestamp expiredDate) {
		this.expiredDate = expiredDate;
	}
}
